import axios from 'axios'
// import { Message, Loading } from 'element-ui'

// const https = require('https')
// 使用create方法创建axios实例
const _axios = axios.create({
  timeout: 7000, // 请求超时时间
  baseURL: 'http://127.0.0.1:8010', // 注意结尾不要/
  method: 'get',
  headers: {
    'Content-Type': 'application/json;charset=UTF-8'
  }
})
// 添加请求拦截器
_axios.interceptors.request.use(config => {
  // loadingInstance = Loading.service({
  //   lock: true,
  //   text: 'loading...'
  // })
  // config.data.token='123'

  return config
})
// 添加响应拦截器
_axios.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.log('KM: error', error)
    debugger
    if (error.response.status === 400) {
      return Promise.reject(error.response.data)
    } else {
      alert('发生未知错误')
      return Promise.reject(error) // 一个给定原因了的被拒绝的 Promise。
    }
  }
)
function request () { }

request.get = function (url, params) {
  return _axios({ url: url, params: params })
}
request.post = function (url, params) {
  return _axios({
    url: url,
    method: 'POST',
    data: JSON.stringify(params)
  })
}
request.put = function (url, params) {
  return _axios({
    url: url,
    method: 'PUT',
    data: JSON.stringify(params)
  })
}
request.delete = function (url, params) {
  return _axios({ url: url, method: 'DELETE', params: params })
}
export default {
  install: function (Vue) {
    Vue.prototype.$requestx = request
  }
}
